<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir."/inc/database.php";
require_once $basedir."/inc/mitarbeiter.php";

class MitarbeiterAbwesend {
    
    public static $TABLENAME = "mitarbeiterabwesend";
    public static $CLASSNAME = "MitarbeiterAbwesend";
    
    private $id = "";
    private $reason = "";
    private $mitarbeiter_id = "";
    private $anfang_am = "0000-00-00";
    private $ende_am = "0000-00-00";
    private $krankmeldung_ok = false;
    private $bemerkung = "";
    
    public function __construct($id="") {
        if($id!='') {
            $temp = Database::getDefault()->openId(MitarbeiterAbwesend::$TABLENAME,$id);
            foreach($temp as $key => $value) {
                $this->$key = $value;
            }
        }
    }
    
    public function getJsonData() {
        $var = get_object_vars($this);
        foreach($var as &$value) {
            if(is_object($value) && method_exists($value, 'getJsonData')) {
                $value = $value->getJsonData();
            }
        }
        return $var;
    }
    
    public function delete() {
        Database::getDefault()->delete(MitarbeiterAbwesend::$TABLENAME,$this->id);
    }
    
    public function save() {
        $values = array();
        foreach ($this as $key => $value) {
            $values[$key] = $value;
        }
        if($this->id=="") {
            $this->id = Database::getDefault()->insert(MitarbeiterAbwesend::$TABLENAME, $values);
        } else {
            Database::getDefault()->update(MitarbeiterAbwesend::$TABLENAME, $values, $this->id);
        }
    }
    
    /**
     * Array mit moeglichen Gruenden als Strings
     */
    static public function getReasons() {
        $array = array('Unbekannt','Krankheit','Kind krank','Urlaub','Sonstiges');
        return $array;
    }
    
    public function getId() {
        return $this->id;
    }
    
    public function setReason($value) {
        $this->reason = $value;
    }
    
    public function getReason() {
        return $this->reason;
    }
    
    public function setMitarbeiterId($value) {
        $this->mitarbeiter_id = $value;
    }
    
    public function getMitarbeiterId() {
        return $this->mitarbeiter_id;
    }
    
    public function getMitarbeiter() {
        return new Mitarbeiter($this->getMitarbeiterId());
    }

    public function setAnfangAm($value) {
        $this->anfang_am = $value;
    }
    
    public function getAnfangAm() {
        return $this->anfang_am;
    }
    
    public function setEndeAm($value) {
        $this->ende_am = $value;
    }
    
    public function getEndeAm() {
        return $this->ende_am;
    }
    
    public function setKrankmeldungOk($value) {
        $this->krankmeldung_ok = $value;
    }
    
    public function isKrankmeldungOk() {
        return $this->krankmeldung_ok;
    }
    
    public function setBemerkung($value) {
        $this->bemerkung = $value;
    }
    
    public function getBemerkung() {
        return $this->bemerkung;
    }
    
    /**
     * Array mit Objekten von MitarbeiterAbwesend
     * 
     * @param $mitarbeiter_id ID des Mitarbeiters
     * @param $nurOffene Nur die nicht entschuldigten
     */
    public static function getAll($mitarbeiter_id="", $nurOffene=true) {
        global $VEREIN;
        $query = "SELECT * FROM " . $VEREIN['prefix'] . MitarbeiterAbwesend::$TABLENAME;
        if($nurOffene || $mitarbeiter_id!="") {
            $query .= " WHERE";
        }
        if($nurOffene) {
            $query .= " (krankmeldung_ok='0' AND reason <> 'Urlaub')";
        }
        if($mitarbeiter_id!="") {
            if($nurOffene) {
                $query .= " AND";
            }
            $query .= " mitarbeiter_id='$mitarbeiter_id'";
        }
        $query .= " ORDER BY anfang_am, ende_am";
        return Database::getDefault()->execute($query, MitarbeiterAbwesend::$CLASSNAME);
    }
    
    /**
     * create table for banking contacts or update if something to do
     */
    public static function initDatabase() {
        global $VEREIN;
        echo "<b>update / create " . MitarbeiterAbwesend::$TABLENAME . "</b><br />";
        $query = "CREATE TABLE IF NOT EXISTS " . $VEREIN['prefix'] . MitarbeiterAbwesend::$TABLENAME . " (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY)";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            die(Database::getDefault()->getConnection()->error);
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . MitarbeiterAbwesend::$TABLENAME . " ADD COLUMN reason VARCHAR(80) NOT NULL";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . MitarbeiterAbwesend::$TABLENAME . " ADD COLUMN mitarbeiter_id INT NOT NULL";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . MitarbeiterAbwesend::$TABLENAME . " ADD COLUMN anfang_am DATE NOT NULL";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . MitarbeiterAbwesend::$TABLENAME . " ADD COLUMN ende_am DATE DEFAULT '0000-00-00'";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . MitarbeiterAbwesend::$TABLENAME . " ADD COLUMN krankmeldung_ok TINYINT(1) NOT NULL DEFAULT 0";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . MitarbeiterAbwesend::$TABLENAME . " ADD COLUMN bemerkung TEXT DEFAULT NULL";
        echo $query . "<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
       echo "<br />";
    }
}
?>